<template>
	<view>
		<web-view :src="gmapSrc" @message="getMessage"></web-view>
	</view>
</template>

<script>
	import { TOKENNAME, APP_LANG, DEFAULT_LANG } from '@/config.js'
	import permision from "@/utils/permission.js"
			
	export default {
		data() {
			return {
				webview: null,
				gmapSrc: '',
				interval: null,
			}
		},
		computed: {
			contactStatus() {
				return this.$store.state.common.contactStatus
			}
		},
		onShow() {
			this.lastLocation = this.locationLatlng
			this.gmapSrc = this.changeLang()
		},
		onLoad() {
			this.gmapSrc = this.changeLang()
			let timer = setTimeout(() => {
				clearTimeout(timer)
				this.$store.commit('UPDATE_TABBAR_VISIBLE_TYPE', 'user')
			}, 100)
			this.changeWebViewBottom()
		},
		methods: {
			// 切换语言
			changeLang() {
				// let baseUrl = `http://mrptmaph5.leaf-tech.net/index.html`
				let baseUrl = `/static/pages/map_shop/index.html`
				let str = `${ baseUrl }?token=${ uni.getStorageSync(TOKENNAME) }&latlng=${ this.locationLatlng }&locationStatus=${ this.locationStatus }&contactStatus=${ this.contactStatus }&type=map&lang=`
				// 进行语言更换
				return str + (uni.getStorageSync(APP_LANG) || DEFAULT_LANG)
			},
			// 响应数据
			changeWebViewBottom() {
				// 获取当前屏幕属性
				const sysInfo = uni.getSystemInfoSync()
				const { screenHeight, statusBarHeight, windowBottom } = sysInfo
				
				setTimeout(() => {
					var currentWebview = this.$scope.$getAppWebview()
					this.webview = currentWebview.children()[0]
					this.webview.setStyle({
						height: (screenHeight - statusBarHeight - 50) + 'px',
						top: statusBarHeight + 'px',
						bottom: 50 + 'px'
					})
					this.webview.show()
				}, 300)
			},
			async getMessage(e) {
				// 解析H5回传的消息
				let data = e.detail.data[0]
				console.log(data)
				let hasKey = k => data.hasOwnProperty(k)
				if (data) {
					if (hasKey('item')){
						// 跳转到商家详情
						this.jumpUrl(`/pages/user/comerciante/detail/detail?merchantId=${ data.item.merchantId }`)
					} else if (hasKey('phone')) {
						this.callAction(data.phone)
					} else if (hasKey('openSetting')) {
						let { type, info } = data.openSetting
						if (type === 'contact') {
							await this.authContact()
							this.gmapSrc = this.changeLang()
							if (this.contactStatus) {
								this.callAction(info)
							}
						} else {
							permision.gotoAppPermissionSetting()
						}
					}
				}
			}
		}
	}
</script>

<style>

</style>